<?
ob_start();
include_once('../includes/loader.php');

if (isset($_POST) && !empty($_POST)) {
	$params = $_POST;
	$action = @$params['action'];
	switch ($action) {
		case 'adddevice':
			$device = new objDevice($params);
			if ($device->determineIP() === false) {
				echo json_encode(array(
					'success' => false,
					'msg' => 'Unable to find IP Address',
					'hostname' => $device->hostname,
					'ip' => $device->ip_address
				));
			} else {
				$subAction = @$_POST['subAction'];
				switch ($subAction) {
					case 'ping':
						$time = $device->ping(3,5,false);
						if ($time > 0) {
							$output = array('success'=>true,'msg' => $device->ip_address.' ('.$time.'ms)');
						} else {
							$output = array('success'=>false,'msg' => 'No Response');
						}
						break;
					case 'snmp':
						$result = $device->checkSNMP();
						if ($result === false) {
							$output = array('success'=>false,'msg'=>'Device did not respond to SNMP','snmp'=>$device->snmp);
						} else {
							$output = array('success'=>true,'msg' => $result);
						}
						break;
					case 'create':
						if ($device->save()) {
							$output = array('success'=>true,'msg'=>'Device successfully added for monitoring','id'=>$device->id);
						} else {
							$output = array('success'=>false,'msg' => 'Error Occured Creating Device');
						}
						break;
					default:
						$output=array('success'=>false,'msg'=>'Action Not Found','params'=>$params);
						break;
				}
				echo json_encode($output);
				break;
			}
	}
} else {
	$nms->setPageVariables(@$_GET);
	include($nms->getTemplatePath());
}
ob_end_flush();
?>
